﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Xml;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YJKCodeBase.WinForm;
using YJKOpeningCommon.DataEnum;
using YJKOpeningCommon.DbOpening;
using YJKOpeningCommon.OpeningProfile;
using YJKOpeningCommon.Public;

namespace YJKOpeningCreate.ConfigForm
{
	public partial class OpeningCreateReportForm : ConfigDataBaseForm
	{
		public OpeningCreateReportForm(UIDocument uiDoc, WindowHandle _hWndRevit)
		{
			this.m_uiDoc = uiDoc;
			this.m_HWndRevit = _hWndRevit;
			this.InitializeComponent();
			base.SetFormLocation(this.strXmlPath);
			this.dgBuild.RowHeadersVisible = false;
			this.dgBuild.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
			this.dgStructure.RowHeadersVisible = false;
			this.dgStructure.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
			WinDPIManager.GetWinFormXYDpi(ref this.m_dXScale, ref this.m_dYScale);
			this.m_iInitExWidth = Convert.ToInt32(900.0 * this.m_dXScale);
			this.m_iInitUnExWidth = Convert.ToInt32(340.0 * this.m_dXScale);
			this.m_iHeight = Convert.ToInt32(550.0 * this.m_dYScale);
			base.Width = this.m_iInitUnExWidth;
			base.Height = this.m_iHeight;
			this.txtMinSize.Text = this.m_iMinSize.ToString();
			this.SetControlPosition();
			this.SetDataGridViewWidth();
		}

		public void TranslateOpeningDataToForm(LinkedList<DbOpeningBase> lstNoWoringStruOpening, LinkedList<DbOpeningBase> lstNoWoringBldOpening, LinkedList<DbOpeningBase> lstWoringStruOpening, LinkedList<DbOpeningBase> lstWoringBldOpening)
		{
			this.m_lstNoWoringStruOpening = lstNoWoringStruOpening;
			this.m_lstNoWoringBldOpening = lstNoWoringBldOpening;
			this.m_lstWoringStruOpening = lstWoringStruOpening;
			this.m_lstWoringBldOpening = lstWoringBldOpening;
		}

		private void btnExpansion_Click(object sender, EventArgs e)
		{
			if (this.btnExpansion.Text == "-->")
			{
				this.btnExpansion.Text = "<--";
				base.Width = this.m_iInitExWidth;
				return;
			}
			this.btnExpansion.Text = "-->";
			base.Width = this.m_iInitUnExWidth;
			base.Height = this.m_iHeight;
		}

		private void SetControlPosition()
		{
			if (this.pnlFunc.Left < this.pnlSelect.Right)
			{
				this.pnlSelect.Location = new System.Drawing.Point(this.pnlSelect.Location.X, this.btnExpansion.Location.Y - this.pnlFunc.Height);
				this.pnlFunc.Location = new System.Drawing.Point(this.pnlSelect.Location.X, this.btnExpansion.Location.Y);
			}
			else
			{
				this.pnlSelect.Location = new System.Drawing.Point(this.pnlSelect.Location.X, this.btnExpansion.Location.Y);
				this.pnlFunc.Location = new System.Drawing.Point(this.btnExpansion.Location.X - this.pnlFunc.Width, this.btnExpansion.Location.Y);
			}
			if (this.tctlOpeningReport.SelectedIndex == 0)
			{
				this.pnlMinSize.Location = new System.Drawing.Point(this.pnlMinSize.Location.X, this.pnlSelect.Location.Y - this.pnlFunc.Height - 3);
				this.tctlOpeningReport.Height = this.pnlMinSize.Top - this.tctlOpeningReport.Top;
				return;
			}
			this.tctlOpeningReport.Height = this.pnlSelect.Top - this.tctlOpeningReport.Top;
		}

		private void OpeningCreateReportForm_Resize(object sender, EventArgs e)
		{
			this.SetControlPosition();
			this.SetDataGridViewWidth();
		}

		private void SetDataGridViewWidth()
		{
			bool visible = true;
			double num = (double)base.Width;
			if (this.btnExpansion.Text == "-->")
			{
				visible = false;
				double num2 = num / 8.0;
				if (this.dgBuild.ColumnCount > 8)
				{
					this.dgBuild.Columns[0].Width = Convert.ToInt32(num2 * 0.8);
					this.dgBuild.Columns[1].Width = Convert.ToInt32(num2 * 0.8);
					this.dgBuild.Columns[2].Width = Convert.ToInt32(num2 * 0.8);
					this.dgBuild.Columns[3].Width = Convert.ToInt32(num2 * 0.8);
					this.dgBuild.Columns[4].Width = Convert.ToInt32(num2 * 1.4);
					this.dgBuild.Columns[8].Width = Convert.ToInt32(num2 * 3.6);
				}
				if (this.dgStructure.ColumnCount > 8)
				{
					this.dgStructure.Columns[0].Width = Convert.ToInt32(num2 * 0.8);
					this.dgStructure.Columns[1].Width = Convert.ToInt32(num2 * 0.8);
					this.dgStructure.Columns[2].Width = Convert.ToInt32(num2 * 0.8);
					this.dgStructure.Columns[3].Width = Convert.ToInt32(num2 * 0.8);
					this.dgStructure.Columns[4].Width = Convert.ToInt32(num2 * 1.4);
					this.dgStructure.Columns[8].Width = Convert.ToInt32(num2 * 3.6);
				}
			}
			else
			{
				double num3 = num / 20.0;
				if (this.dgStructure.ColumnCount > 10)
				{
					this.dgBuild.Columns[0].Width = Convert.ToInt32(num3 * 0.8);
					this.dgBuild.Columns[1].Width = Convert.ToInt32(num3 * 0.8);
					this.dgBuild.Columns[2].Width = Convert.ToInt32(num3 * 0.8);
					this.dgBuild.Columns[3].Width = Convert.ToInt32(num3 * 0.8);
					this.dgBuild.Columns[4].Width = Convert.ToInt32(num3 * 1.4);
					this.dgBuild.Columns[5].Width = Convert.ToInt32(num3 * 1.4);
					this.dgBuild.Columns[6].Width = Convert.ToInt32(num3 * 1.4);
					this.dgBuild.Columns[7].Width = Convert.ToInt32(num3 * 3.0);
					this.dgBuild.Columns[8].Width = Convert.ToInt32(num3 * 3.0);
					this.dgBuild.Columns[9].Width = Convert.ToInt32(num3 * 3.0);
					this.dgBuild.Columns[10].Width = Convert.ToInt32(num3 * 4.0);
				}
				if (this.dgStructure.ColumnCount > 10)
				{
					this.dgStructure.Columns[0].Width = Convert.ToInt32(num3 * 0.8);
					this.dgStructure.Columns[1].Width = Convert.ToInt32(num3 * 0.8);
					this.dgStructure.Columns[2].Width = Convert.ToInt32(num3 * 0.8);
					this.dgStructure.Columns[3].Width = Convert.ToInt32(num3 * 0.8);
					this.dgStructure.Columns[4].Width = Convert.ToInt32(num3 * 1.4);
					this.dgStructure.Columns[5].Width = Convert.ToInt32(num3 * 1.4);
					this.dgStructure.Columns[6].Width = Convert.ToInt32(num3 * 1.4);
					this.dgStructure.Columns[7].Width = Convert.ToInt32(num3 * 3.0);
					this.dgStructure.Columns[8].Width = Convert.ToInt32(num3 * 3.0);
					this.dgStructure.Columns[9].Width = Convert.ToInt32(num3 * 3.0);
					this.dgStructure.Columns[10].Width = Convert.ToInt32(num3 * 4.0);
				}
			}
			if (this.dgBuild.ColumnCount > 10)
			{
				this.dgBuild.Columns[5].Visible = visible;
				this.dgBuild.Columns[6].Visible = visible;
				this.dgBuild.Columns[7].Visible = visible;
				this.dgBuild.Columns[9].Visible = visible;
				this.dgBuild.Columns[10].Visible = visible;
			}
			if (this.dgStructure.ColumnCount > 10)
			{
				this.dgStructure.Columns[5].Visible = visible;
				this.dgStructure.Columns[6].Visible = visible;
				this.dgStructure.Columns[7].Visible = visible;
				this.dgStructure.Columns[9].Visible = visible;
				this.dgStructure.Columns[10].Visible = visible;
			}
		}

		public void SetOpeningData2DataGridView()
		{
			this.SetOpeningData2DataGridView(this.m_lstWoringStruOpening, "结构", true);
			this.SetOpeningData2DataGridView(this.m_lstNoWoringStruOpening, "结构", false);
			this.SetOpeningData2DataGridView(this.m_lstWoringBldOpening, "建筑", true);
			this.SetOpeningData2DataGridView(this.m_lstNoWoringBldOpening, "建筑", false);
		}

		public void SetOpeningData2DataGridView(LinkedList<DbOpeningBase> lstDbOpening, string strTabName, bool bIsAllowCheck)
		{
			DataGridView dataGridView;
			if (strTabName == "建筑")
			{
				dataGridView = this.dgBuild;
			}
			else
			{
				dataGridView = this.dgStructure;
			}
			dataGridView.AllowUserToAddRows = true;
			foreach (DbOpeningBase dbOpeningBase in lstDbOpening)
			{
				if (dbOpeningBase.m_eOpeningState != EOpeningState.eOpen)
				{
					string strAxisX = dbOpeningBase.m_strAxisX;
					string strAxisY = dbOpeningBase.m_strAxisY;
					string strFloorPlan = dbOpeningBase.m_strFloorPlan;
					EHostObject eHostObject = dbOpeningBase.m_eHostObject;
					int num = (int)eHostObject;
					string text = StrHostObj.m_strHostObj[num];
					ECutObject eCutObject = dbOpeningBase.m_eCutObject;
					num = (int)eCutObject;
					string text2 = StrCutObj.m_strCutObj[num];
					string text3 = string.Empty;
					if ((dbOpeningBase.m_eCutObject == ECutObject.eHvPipe || dbOpeningBase.m_eCutObject == ECutObject.eWaterPipe || dbOpeningBase.m_eCutObject == ECutObject.ePipeMulti) && dbOpeningBase is DbRectWallOpening)
					{
						OpeningProfileRect openingProfileRect = dbOpeningBase.m_profile as OpeningProfileRect;
						text3 = AssistFunc.feetToMM(openingProfileRect.m_cutHeight).ToString("f0");
					}
					else
					{
						text3 = dbOpeningBase.GetCutSize(false);
					}
					string openingSize = dbOpeningBase.GetOpeningSize(false);
					string strDesignDate = dbOpeningBase.m_strDesignDate;
					string strRemark = dbOpeningBase.m_strRemark;
					dataGridView.Rows.Add(new object[]
					{
						true,
						dataGridView.RowCount,
						strAxisX,
						strAxisY,
						text,
						text2,
						strFloorPlan,
						text3,
						openingSize,
						strDesignDate,
						strRemark
					});
					dataGridView.Rows[dataGridView.RowCount - 2].Tag = dbOpeningBase;
					if (!bIsAllowCheck)
					{
						dataGridView.Rows[dataGridView.RowCount - 2].Cells[1].Tag = "DocHasNoWorkingPower";
					}
					if (strTabName == "建筑" && bIsAllowCheck)
					{
						this.SetRowCheckBoxCheckOpeningSize(dataGridView.Rows[dataGridView.RowCount - 2]);
					}
					else if (!bIsAllowCheck)
					{
						dataGridView.Rows[dataGridView.RowCount - 2].Cells[0].Value = false;
						dataGridView.Rows[dataGridView.RowCount - 2].Cells[0].ReadOnly = true;
						dataGridView.Rows[dataGridView.RowCount - 2].Cells[0].Tag = "NoWorking";
					}
					else
					{
						dataGridView.Rows[dataGridView.RowCount - 2].Cells[0].Value = true;
						dataGridView.Rows[dataGridView.RowCount - 2].Cells[0].ReadOnly = false;
					}
				}
			}
			dataGridView.AllowUserToAddRows = false;
		}

		private void OpeningCreateReportForm_Load(object sender, EventArgs e)
		{
		}

		private void btnCheckAll_Click(object sender, EventArgs e)
		{
			if (this.tctlOpeningReport.SelectedIndex == 0)
			{
				IEnumerator enumerator = ((IEnumerable)this.dgBuild.Rows).GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						object obj = enumerator.Current;
						DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
						if (!((string)dataGridViewRow.Cells[0].Tag == "NoWorking"))
						{
							dataGridViewRow.Cells[0].Value = true;
						}
					}
					return;
				}
			}
			foreach (object obj2 in ((IEnumerable)this.dgStructure.Rows))
			{
				DataGridViewRow dataGridViewRow2 = (DataGridViewRow)obj2;
				if (!((string)dataGridViewRow2.Cells[0].Tag == "NoWorking"))
				{
					dataGridViewRow2.Cells[0].Value = true;
				}
			}
		}

		private void btnCheckNone_Click(object sender, EventArgs e)
		{
			if (this.tctlOpeningReport.SelectedIndex == 0)
			{
				IEnumerator enumerator = ((IEnumerable)this.dgBuild.Rows).GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						object obj = enumerator.Current;
						DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
						if (!((string)dataGridViewRow.Cells[0].Tag == "NoWorking"))
						{
							dataGridViewRow.Cells[0].Value = false;
						}
					}
					return;
				}
			}
			foreach (object obj2 in ((IEnumerable)this.dgStructure.Rows))
			{
				DataGridViewRow dataGridViewRow2 = (DataGridViewRow)obj2;
				if (!((string)dataGridViewRow2.Cells[0].Tag == "NoWorking"))
				{
					dataGridViewRow2.Cells[0].Value = false;
				}
			}
		}

		private void btnLookUp_Click(object sender, EventArgs e)
		{
			if (this.tctlOpeningReport.SelectedIndex == 0)
			{
				if (this.dgBuild.SelectedRows.Count > 0)
				{
					DbOpeningBase dbOpeningBase = this.dgBuild.Rows[this.dgBuild.SelectedRows[0].Index].Tag as DbOpeningBase;
					CommonInterface.DoubleClick2Location(dbOpeningBase, this.m_uiDoc);
					return;
				}
			}
			else if (this.dgStructure.SelectedRows.Count > 0)
			{
				DbOpeningBase dbOpeningBase2 = this.dgStructure.Rows[this.dgStructure.SelectedRows[0].Index].Tag as DbOpeningBase;
				CommonInterface.DoubleClick2Location(dbOpeningBase2, this.m_uiDoc);
			}
		}

		private void btnCreateOpening_Click(object sender, EventArgs e)
		{
			string columnName = "BldNote";
			DataGridView dataGridView;
			if (this.tctlOpeningReport.SelectedIndex == 0)
			{
				dataGridView = this.dgBuild;
			}
			else
			{
				dataGridView = this.dgStructure;
				columnName = "StruNote";
			}
			this.m_lstNeedOpening.Clear();
			foreach (object obj in ((IEnumerable)dataGridView.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				DbOpeningBase dbOpeningBase = dataGridViewRow.Tag as DbOpeningBase;
				if (dbOpeningBase != null && dataGridViewRow.Cells.Count != 0 && dataGridViewRow.Cells[columnName].Value != null)
				{
					string strRemark = dataGridViewRow.Cells[columnName].Value.ToString();
					dbOpeningBase.m_strRemark = strRemark;
					if (dataGridViewRow.Cells[0].Value != null && (bool)dataGridViewRow.Cells[0].Value)
					{
						this.m_lstNeedOpening.AddLast(dbOpeningBase);
					}
				}
			}
			if (this.m_lstNeedOpening.Count == 0)
			{
				YJKMessageBox.Show("没有选择可开的洞");
				return;
			}
			this.m_bIsCreateOpening = true;
			base.Close();
		}

		public LinkedList<DbOpeningBase> GetNeedCrate3DOpeningData()
		{
			return this.m_lstNeedOpening;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void tctlOpeningReport_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.tctlOpeningReport.SelectedIndex == 0)
			{
				this.pnlMinSize.Visible = true;
			}
			else
			{
				this.pnlMinSize.Visible = false;
			}
			this.SetControlPosition();
		}

		private void txtMinSize_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar != '\b' && (e.KeyChar < '0' || e.KeyChar > '9'))
			{
				e.Handled = true;
			}
		}

		private void dgBuild_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
		{
			if (e.RowIndex < 0)
			{
				return;
			}
			DbOpeningBase dbOpeningBase = this.dgBuild.Rows[e.RowIndex].Tag as DbOpeningBase;
			CommonInterface.DoubleClick2Location(dbOpeningBase, this.m_uiDoc);
		}

		private void dgStructure_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
		{
			if (e.RowIndex < 0)
			{
				return;
			}
			DbOpeningBase dbOpeningBase = this.dgStructure.Rows[e.RowIndex].Tag as DbOpeningBase;
			CommonInterface.DoubleClick2Location(dbOpeningBase, this.m_uiDoc);
		}

		private void txtMinSize_Leave(object sender, EventArgs e)
		{
			if (this.txtMinSize.Text == "")
			{
				this.txtMinSize.Text = "0";
			}
			if (this.m_iMinSize.ToString() == this.txtMinSize.Text)
			{
				return;
			}
			this.m_iMinSize = Convert.ToInt32(this.txtMinSize.Text);
			foreach (object obj in ((IEnumerable)this.dgBuild.Rows))
			{
				DataGridViewRow rowCheckBoxCheckOpeningSize = (DataGridViewRow)obj;
				this.SetRowCheckBoxCheckOpeningSize(rowCheckBoxCheckOpeningSize);
			}
		}

		private void SetRowCheckBoxCheckOpeningSize(DataGridViewRow dr)
		{
			if ((string)dr.Cells[1].Tag == "DocHasNoWorkingPower")
			{
				return;
			}
			string text = string.Empty;
			if (this.tctlOpeningReport.SelectedIndex == 0)
			{
				text = dr.Cells["BldOpeningSize"].Value.ToString();
			}
			string[] array = text.Split(new char[]
			{
				'X'
			});
			bool flag = false;
			foreach (string value in array)
			{
				if ((ulong)Convert.ToUInt32(value) >= (ulong)((long)this.m_iMinSize))
				{
					flag = true;
				}
			}
			if (flag)
			{
				dr.Cells[0].Value = true;
				dr.Cells[0].ReadOnly = false;
				dr.Cells[0].Tag = "Working";
				return;
			}
			dr.Cells[0].Value = false;
			dr.Cells[0].ReadOnly = true;
			dr.Cells[0].Tag = "NoWorking";
		}

		[DllImport("user32.dll")]
		private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SendESC()
		{
			List<Element> selElements = new List<Element>();
			RevitVersionFuncs.SetSelection(this.m_uiDoc.Selection, selElements);
			this.m_uiDoc.RefreshActiveView();
			byte bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			OpeningCreateReportForm.SendMessage(this.m_HWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			OpeningCreateReportForm.keybd_event(bVk, 0, dwFlags, 0);
			OpeningCreateReportForm.keybd_event(bVk, 0, dwFlags2, 0);
		}

		private void OpeningCreateReportForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			XmlDocument xmlDocument = new XmlDocument();
			if (File.Exists(this.strXmlPath))
			{
				XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
				xmlReaderSettings.IgnoreComments = true;
				XmlReader xmlReader = XmlReader.Create(this.strXmlPath, xmlReaderSettings);
				xmlDocument.Load(xmlReader);
				XmlElement documentElement = xmlDocument.DocumentElement;
				documentElement.SetAttribute("LocationX", base.Location.X.ToString());
				documentElement.SetAttribute("LocationY", base.Location.Y.ToString());
				xmlReader.Close();
			}
			else
			{
				XmlDeclaration newChild = xmlDocument.CreateXmlDeclaration("1.0", "gb2312", null);
				xmlDocument.AppendChild(newChild);
				XmlElement xmlElement = xmlDocument.CreateElement("HistoryData");
				xmlDocument.AppendChild(xmlElement);
				xmlElement.SetAttribute("LocationX", base.Location.X.ToString());
				xmlElement.SetAttribute("LocationY", base.Location.Y.ToString());
			}
			xmlDocument.Save(this.strXmlPath);
			this.m_bFormClose = true;
			this.SendESC();
		}

		private void OpeningCreateReportForm_KeyDown(object sender, KeyEventArgs e)
		{
			try
			{
				Keys keyCode = e.KeyCode;
				if (keyCode == Keys.Escape)
				{
					base.Close();
				}
			}
			catch
			{
			}
		}

		public bool m_bIsCreateOpening;

		public bool m_bFormClose;

		private int m_iInitExWidth;

		private int m_iInitUnExWidth;

		private int m_iHeight;

		private int m_iMinSize = 300;

		private LinkedList<DbOpeningBase> m_lstNoWoringStruOpening = new LinkedList<DbOpeningBase>();

		private LinkedList<DbOpeningBase> m_lstNoWoringBldOpening = new LinkedList<DbOpeningBase>();

		private LinkedList<DbOpeningBase> m_lstWoringStruOpening = new LinkedList<DbOpeningBase>();

		private LinkedList<DbOpeningBase> m_lstWoringBldOpening = new LinkedList<DbOpeningBase>();

		private LinkedList<DbOpeningBase> m_lstNeedOpening = new LinkedList<DbOpeningBase>();

		private UIDocument m_uiDoc;

		private double m_dXScale = 1.0;

		private double m_dYScale = 1.0;

		private WindowHandle m_HWndRevit;

		private string strXmlPath = Product.UserDataLocation + "\\OpeningCreateReportForm.xml";
	}
}
